<?php


namespace api\channel\JdUnion;

use Dml\EasyTBK\JingDong\Request\JdUnionPromotionBysubunionidGetRequest;
use think\facade\Log;

/**
 * 商品转链
 * Class PromotionBysubunionidGet
 * @package api\channel\JdUnion
 */
class PromotionBysubunionidGet extends Base
{

    //初始化
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 错误代码示例
     * 文档接口与code地址：https://union.jd.com/openplatform/api/v2?apiName=jd.union.open.promotion.bysubunionid.get
     * @var
     */
    public $codeMsg = [
        '2001208'   =>  'subUnionId入参格式有误',
        '2001923'   =>  '礼金已过期',
        '2001603'   =>  '子联盟没有签订法律协议，协议签署地址：https://union.jd.com/account_son',
        '2001602'   =>  '无母账号权限或未签署协议',
        '2001921'   =>  'skuId不可使用该礼金',
        '2001403'   =>  '无调用权限，请联系联盟运营申请',
        '2001920'   =>  '未开通礼金权限，请联系联盟运营',
        '2001925'   =>  '礼金链接不支持再次转链',
        '2001605'   =>  'pid错误，请线下找子账号核对',
        '2001924'   =>  '礼金已领光',
        '2001201'   =>  '联盟ID错误',
        '2001919'   =>  '礼金不存在',
        '2001917'   =>  '优惠券不可购买该商品，请更换其他优惠券',
        '2001217'   =>  '渠道关系ID与unionid不匹配',
        '2001932'   =>  '礼金信息配置不全，请联系联盟运营',
        '2001216'   =>  '渠道关系ID不存在',
        '2001931'   =>  'pid不存在',
        '2001215'   =>  'pid的第一段与您的unionId不能相同',
        '2001930'   =>  'pid不在该礼金定向范围内',
        '2001214'   =>  'pid和subUnionId不能同时使用', //
        '2001610'   =>  'APPID不存在或者WEBID不存在',
        '2001213'   =>  '传入pid错误',
        '2001212'   =>  '参数错误',
        '200'       =>  '调用成功',
        '403'       =>  '无访问权限，如有疑问请联系联盟客服',
        '2001929'   =>  'unionId不在该礼金定向范围内',
        '2001609'   =>  '服务降级，请稍后重试！',
        '2001928'   =>  '此unionId无定向优惠券转链权限',
        '2001703'   =>  '您填写的原始链接不符合要求',
        '2001504'   =>  '服务降级，请稍后重试',
        '2001702'   =>  '无原始链接转链权限',
        '2001107'   =>  '不允许给此落地页转链，请联系联盟运营',
        '2001701'   =>  '无二合一转链权限',
        '2001905'   =>  '优惠券无效',
        '2001904'   =>  '优惠券接口异常，请稍后重试',
        '2001101'   =>  'materialId链接格式错误',
        '2001105'   =>  '聊天工具禁止推广首页链接',
        '2001501'   =>  'WEBID填写错误',
        '2001104'   =>  '商品不在推广中',
        '2001102'   =>  'materialId链接无法识别',
        '2001912'   =>  '优惠券接口异常，请稍后重试',
        '2001911'   =>  '优惠券消费限额必须小于商品最低价格',
        '2001910'   =>  '系统异常',
        '2001916'   =>  '商品不支持京券',
        '2001915'   =>  '商品不支持东券，请勿传入couponUrl',
        '2001914'   =>  '优惠券剩余数量不足，请更换其他优惠券',
        '2001230'   =>  '参数格式超长',
        '500'       =>  '服务端异常',
        '2001909'   =>  '优惠券与商品不匹配',
        '429'       =>  '接口调用超频，请降低接口调用频次',
        '2001906'   =>  '优惠券已过期',
    ];



    /**
     * Notes:社交媒体获取推广链接
     * User: ymzhang
     * Date: 2022/08/01
     * Time: 11:00
     * @param array $params
     * @return array
     */
    public function jd_union_open_promotion_bysubunionid_get(array $params)
    {
        if (!isset($params['materialId']) || empty($params['materialId'])) {
            return web_request([],false, '2001101', 'materialId不能为空');
        }
        if (!isset($params['subUnionId']) || empty($params['subUnionId'])) {
            return web_request([],false, '2001208', 'subUnionId不能为空');
        }
        $req = new JdUnionPromotionBysubunionidGetRequest();

        $req->setMaterialId($params['materialId']);

        $req->setSubUnionId($params['subUnionId']);

        if (isset($params['positionId'])) {
            $req->setPositionId($params['positionId']);
        }
        if (isset($params['pid'])) {
            $req->setPid($params['pid']);
        }
        //优惠券
        if (isset($params['couponUrl'])) {
            $req->setCouponUrl($params['couponUrl']);
        }

        if (isset($params['chainType'])) {
            $req->setChainType($params['chainType']);
        }

        if (isset($params['channelId'])) {
            $req->setChannelId($params['channelId']);
        }

        $result = $this->jd->execute($req);

        if (isset($result['error_response'])) {
            Log::error($result['error_response']['zh_desc']);
            return web_request([],false, $result['error_response']['code'], $result['error_response']['zh_desc']);
        }
        $getResult = json_decode($result[$req->getOutputParameters()]['getResult'], true);
        if ($code = $getResult['code'] != 200) {
            return web_request([],false, $code, $this->getMsg($code));
        }
        return web_request($getResult['data'],true, 200, $this->getMsg('200'));
    }
}